﻿using System;
using System.Web.UI;
using AMS.Web.BaseData.Helper;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;

namespace AMS.Web.UserControls
{
    public partial class NavigateCtl : UserControl
    {
        private const string m_Fg = "&nbsp;<img src=\"../Images/navigate.gif\" alt=\"\" href=\"#\" />&nbsp;";
        private readonly CreateControlHelper m_CreateCtlHlp = new CreateControlHelper();
        private ObjXmlReader m_XmlReader;

        private string ClassName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("ClassName", Page); }
        }

        private string TypeName
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("TypeName", Page); }
        }

        /// <summary>
        /// 
        /// </summary>
        public string SettingXmlPath
        {
            get
            {
                if (hdnSettingXmlPath.Value.Length == 0)
                    throw new ArgumentException("请设置导航栏控件的SettingXmlPath属性");
                return hdnSettingXmlPath.Value;
            }
            set { hdnSettingXmlPath.Value = value; }
        }

        private string MenuId
        {
            get { return m_CreateCtlHlp.ReturnQueryStringValue("MenuId", Page); }
        }

        private int menuAmount = 0;

        private string m_HeadNav = "";

        private Menu GetMenu()
        {
            Menu mu = null;
            var acc = new BaseDataAccess();
            try
            {
                object objMenu = acc.GetSingleObjectById(typeof (Menu).Name, MenuId);
                if (objMenu != null && objMenu is Menu)
                    mu = (Menu) objMenu;
            }
            finally
            {
                acc.CloseSession();
            }
            return mu;
        }

        private void SetHeadNavItem(Menu menu)
        {
            if (menu == null)
            {
                menuAmount = 0;
                return;
            }
            string strItem = menu.Name;
            if (menuAmount > 0)
                m_HeadNav = strItem + m_Fg + m_HeadNav;
            else
                m_HeadNav = strItem;
            menuAmount++;
            SetHeadNavItem(menu.Parent);
        }


        private void SetPageNav(string strClassMemo, string strTypeMemo, string strPageName, string strListPageName, string strEditPageName,string exMemo)
        {
            if (strPageName.EndsWith(strListPageName.ToLower()) || strPageName.EndsWith(strEditPageName.ToLower()))
            {
                string str = "您当前的位置：";
                str += m_HeadNav;
                if (!string.IsNullOrEmpty(strTypeMemo))
                {
                    strTypeMemo += exMemo;
                    if (strPageName.EndsWith(strListPageName.ToLower()))
                        str += m_Fg + strTypeMemo + "列表";
                    else if (strPageName.EndsWith(strEditPageName.ToLower()))
                    {
                        str += m_Fg + " <a href='" + strListPageName + "?MenuId=" + MenuId + "&ClassName=" + ClassName + "&TypeName=" + TypeName + "'>" +
                               strTypeMemo +
                               "列表</a> ";
                        str += m_Fg + strTypeMemo + "编辑";
                    }
                }
                else
                {
                    strClassMemo += exMemo;
                    if (strPageName.EndsWith(strListPageName.ToLower()))
                        str += m_Fg + strClassMemo + "列表 ";
                    else if (strPageName.EndsWith(strEditPageName.ToLower()))
                    {
                        str += m_Fg + "<a href='" + strListPageName + "?MenuId=" + MenuId + "&ClassName=" + ClassName + "'>" + strClassMemo +
                               "列表</a>";
                        str += m_Fg + strClassMemo + "编辑";
                    }
                }
                lblNav.Text = str;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                SetHeadNavItem(GetMenu());
                var m_LoadHelper = new PageLoadHelper(SettingXmlPath);
                if (m_XmlReader == null)
                    m_XmlReader = m_LoadHelper.XmlReader;
                string strTypeMemo = m_XmlReader.GetNodeAttributeValue(ClassName, TypeName, m_LoadHelper.MEMO, true);
                string strClassMemo = m_XmlReader.GetNodeAttributeValue(ClassName, m_LoadHelper.MEMO, true);
                if (!string.IsNullOrEmpty(strClassMemo))
                {
                    string strPageName = Page.Request.Url.LocalPath.ToLower();
                    SetPageNav(strClassMemo, strTypeMemo, strPageName,"ObjList.aspx","ObjEdit.aspx","");
                    SetPageNav(strClassMemo, strTypeMemo, strPageName,"WoList.aspx","BaseInfo.aspx","单据");
                }
            }
        }
    }
}